/************ Site Theme Switching and Controlling *************************************************/
/************ BEGIN - Theme Switching ************************************************/
/** NOTE: 
	Attach setSiteTheme(siteThemeIdentifier); on "form_init" of site landing page
	Example usage 
	1. function Form_Init(eventObject)
	{
		setSiteTheme("siteThemeIdentifier", "defaultTheme", "override");
	}
	2. function Form_Init(eventObject)
	{
		setSiteTheme("mysite", "base", "override");
	}	
	Explanation: 
	siteThemeIdentifier (mandatory)	: 	The identifier that stores the site theme in the user preference. The value for this variable comes as the url string to identify the theme.   
	defaultTheme  (optional)	: 	the themepack name that can be set as default if no user preference is present.
									this is same as the path of custom css included in the caf files e.g, /cordys/wcp/theme/Qontym/
									useful in case the default theme needs to be set other than the custom css path
	override (optional)			: 	if present, the user preferences are ignored, and the name specified in the defaultTheme is taken
									possible value is "override", it is ignored if left blank
	
	**/
// To set / clear theme based on the user preference
function setSiteTheme(siteThemeIdentifier, defaultTheme, override)
{
	var positionPreferences = CordysRoot.document.getElementById("preferences");
	var siteTheme = positionPreferences.getProperty(siteThemeIdentifier);
	var existingTheme = getParam(top.location.href, siteThemeIdentifier);

	if (!system.parameters[".caf"]) system.parameters[".caf"] = new Object();
	system.parameters[".caf"][siteThemeIdentifier] = siteTheme;

	if (defaultTheme)
	{
		if (override == "override")
		{
			//if override is set the user preference siteTheme is replaced by defaultTheme
			siteTheme = defaultTheme;
		}
		else
		{
			//if no override is passed, the user preferences take precedence
			siteTheme = siteTheme ? siteTheme : defaultTheme;
		}
	}


	if (siteTheme && (!existingTheme || existingTheme != siteTheme))
	{
		top.location.replace(setParam(top.location.href, siteThemeIdentifier, siteTheme));
	}

	if (!siteTheme && existingTheme)
	{
		top.location.replace(setParam(top.location.href, siteThemeIdentifier, null));
	}

}

// To get parameter value from url
function getParam(url, param)
{
	var queryString = url.split("#")[0].split("?")[1];

	if (!queryString) return;

	var params = queryString.split("&");
	for (var i = 0; i < params.length; i++)
	{
		if (params[i].split("=")[0] == param) return params[i].split("=")[1];
	}
}

// To set parameter value to url
function setParam(url, key, value)
{
	var hashSplit = url.split("#");
	var querySplit = hashSplit[0].split("?");
	var queryString = querySplit[1];
	var newParam = value ? (key + "=" + value) : "";
    if(queryString)
	{
		var params = queryString.split("&");
		var matchCount = 0;
		for(var i=0; i<params.length; i++)
		{
			if(params[i].split("=")[0] == key)
			{
				if(newParam)
				{
					params[i] = newParam;
					matchCount++;
					break;
				}
				else
				{
					params.splice(i, 1);
					matchCount++;
					break;
				}
			}
		}
		if(matchCount == 0 && newParam) params[params.length] = newParam;
		queryString = params.join("&");
	}
	else
	{
		queryString = newParam;
	}
	//  FORMAT:  baseURL + "?" + queryString + "#" + postURL
	return querySplit[0] + (queryString ? ("?" + queryString) : "") + (hashSplit[1] ? "#" + hashSplit[1] : "");
}

/************ END - Theme Switching ************************************************/


/************ BEGIN - Theme Controller ************************************************/
/** NOTE: Attach "initializeThemeController(avThemeController, avThemeControllerSchema, avThemesCollectionData, avThemePath, avThemeLocationPath, avsiteThemeIdentifier, avSiteDefaultThemeLocation) " on "form_init_done" **/
/** NOTE: Attach "onThemeSelect(eventObject)" on onclick and ondblclick of avThemeController **/

// Global variables for Theme Controller(tc)
var tcsiteThemeIdentifier;
var tcThemeLocationPath;

// initializeThemeController - To be invoked from "form_init_done"
function initializeThemeController(avThemeController, avThemeControllerSchema, avThemesCollectionData, avThemePath, avThemeLocationPath, avsiteThemeIdentifier, avSiteDefaultThemeLocation)
{
	// Set global variables
	tcsiteThemeIdentifier = avsiteThemeIdentifier;
	tcThemeLocationPath = avThemeLocationPath;

	var positionPreferences = CordysRoot.document.getElementById("preferences");
	var themes = cordys.selectXMLNodes(cordys.cloneXMLDocument(avThemesCollectionData.XMLDocument), avThemePath);
	avThemeController.removeArtifacts();
	var artifactFactory = avThemeController.addArtifactFactoryType("wcp.library.util.ArtifactFactoryType");
	artifactFactory.artifactSchema = cordys.cloneXMLDocument(avThemeControllerSchema.XMLDocument).documentElement;
	artifactFactory.addArtifacts(themes);
	avThemeController.selectArtifactByIndex(getNodeIndex(themes, avThemeLocationPath, positionPreferences.getProperty(avsiteThemeIdentifier) ? positionPreferences.getProperty(avsiteThemeIdentifier) : avSiteDefaultThemeLocation));
}

// onThemeSelect - To be invoked from onclick and ondblclick of avThemeController
function onThemeSelect(eventObject)
{
	var siteTheme = cordys.getNodeText(eventObject.artifactObject.launchingInfo, tcThemeLocationPath, "");
	var positionPreferences = CordysRoot.document.getElementById("preferences");
	positionPreferences.putProperty(tcsiteThemeIdentifier, siteTheme);
	positionPreferences.save();
	application.confirm("To view the theme changes... \nThe webpage has to be reloaded.\nDo you want to reload ?", false, closeHandler, false, "Confirm Message");
	eventObject.returnValue = false;
}

function closeHandler(confirmReturnValue)
{
	if (confirmReturnValue == 1)
	{
		setSiteTheme(tcsiteThemeIdentifier);
	}
}

// To get the index of the selected theme
function getNodeIndex(nodeList, xpath, value)
{
	var index = -1;

	if (value) for (var i = 0; i < nodeList.length; i++)
	{
		if (cordys.getNodeText(nodeList[i], xpath) == value)
		{
			index = i;
			break;
		}
	}

	return index;
}

/************ END - Theme Controller ************************************************/